﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using AMS.Database;
using AMS.Business;
using AMS.UI;

namespace AMS
{
    public partial class RequestorListControl : UserControl
    {
        private Int32 selectedValue = -1;
        private String selectedName = String.Empty;
        private DataTable requestors;
        private List<RequestorClass> requestorList;
        private String requestorDetails = String.Empty;

        public delegate void RequestorSelectionChangedHandler(object sender, RequestorArgs e);
        public event RequestorSelectionChangedHandler RequestorSelectionChanged;

        #region user control stuff
        public RequestorListControl()
        {
            InitializeComponent();

            cboRequestors.Width = this.Width;
            cboRequestors.Height = this.Height;
        }
        #endregion

        public Int32 SelectedValue
        {
            get { return selectedValue; }
            set 
            { 
                selectedValue = value;

                if (value != -1)
                {
                    #region Old code
                    //DataView dv = new DataView(requestors);
                    //dv.RowFilter = "RequestorID = " + selectedValue.ToString();

                    //IEnumerator enumerator = dv.GetEnumerator();
                    //while (enumerator.MoveNext())
                    //{
                    //    DataRowView drv = (DataRowView)enumerator.Current;
                        
                    //    // Set the properties on the combobox
                    //    cboRequestors.SelectedValue = selectedValue;

                    //    break;
                    //}
                    #endregion

                    foreach (RequestorClass requestorClass in requestorList)
                    {
                        if (Convert.ToInt32(requestorClass.RequestorID) == value)
                        {
                            cboRequestors.SelectedValue = requestorClass.RequestorID;
                            break;
                        }
                    }
                }
            }
        }

        public String SelectedName
        {
            get { return selectedName; }
        }

        public Int32 SelectedIndex
        {
            get { return cboRequestors.SelectedIndex; }
            set { cboRequestors.SelectedIndex = value; }
        }

        public void RefreshData()
        {
            FillRequestors();
        }

        private void FillRequestors()
        {
            requestorList = DataAccess.GetRequestorList();

            cboRequestors.DataSource = null;
            cboRequestors.DataSource = requestorList;
            cboRequestors.DisplayMember = "RequestorName";
            cboRequestors.ValueMember = "RequestorID";
        }

        private void SelectRequestor(object sender, EventArgs e)
        {
            if (cboRequestors.SelectedIndex != -1)
            {
                RequestorClass requestorClass = requestorList[cboRequestors.SelectedIndex];
                //DataRowView view = (DataRowView)cboRequestors.Items[cboRequestors.SelectedIndex];

                selectedValue = Convert.ToInt32(requestorClass.RequestorID);
                selectedName = requestorClass.RequestorName;
                requestorDetails = requestorClass.Details;
                //selectedValue = Convert.ToInt32(view.Row[0]);
                //selectedName = view.Row[1].ToString();
                //requestorDetails = view.Row[2].ToString();

                RequestorArgs requestorArgs = new RequestorArgs(selectedValue, selectedName, cboRequestors.SelectedIndex);
                RequestorSelectionChanged(this, requestorArgs);

                tip.Show(requestorDetails, this);
            }
            else
            {
                selectedValue = -1;
                selectedName = String.Empty;
                tip.RemoveAll();
            }
        }

        #region user control stuff
        private void Resize(object sender, EventArgs e)
        {
            cboRequestors.Width = this.Width;
            cboRequestors.Height = this.Height;
        }

        private void cboRequestors_Leave(object sender, EventArgs e)
        {
            tip.RemoveAll();
        }
        #endregion
    }


    #region Cosmetic stuff
    public class RequestorArgs : System.EventArgs
    { 
        private Int32 selectedValue;
        private String selectedName = String.Empty;
        private Int32 selectedIndex;

        public RequestorArgs(Int32 SelectedValue, String SelectedName, Int32 SelectedIndex)
        {
            selectedIndex = SelectedIndex;
            selectedName = SelectedName;
            selectedValue = SelectedValue;
        }

        public Int32 SelectedValue { get { return selectedValue; } }
        public String SelectedName { get { return selectedName; } }
        public Int32 SelectedIndex { get { return selectedIndex; } }
    }
    #endregion
}
